import 'dart:math';

import 'package:flutter/material.dart';
import 'package:hc_ui/config/hc_customer_config.dart';
import 'package:hc_ui/config/hc_default_theme_config.dart';
import 'package:hc_ui/theme/hc_base_theme.dart';

import '../utils/hc_color_util.dart';
import 'hc_common_theme.dart';

class HcButtonTheme extends HcBaseTheme<HcButtonTheme> {
  //背景颜色
  Color? _backgroundColor;

  //禁用状态下的背景颜色
  Color? _disabledBackgroundColor;

  //文字颜色
  Color? _textColor;

  //禁用状态下的文字颜色
  Color? _disabledTextColor;

  //水波纹颜色
  Color? _splashColor;

  //高亮颜色
  Color? _highlightColor;

  //最小大小
  Size? _miniSize;

  //最大大小
  Size? _maxSize;

  EdgeInsets? _padding;

  //按钮的圆角
  BorderRadius? _borderRadius;

  //文字大小
  double? _fontSize;

  //最大行数
  int? _maxLines;

  //文字粗细
  FontWeight? _fontWeight;

  //边框颜色
  BorderSide? _side;

  //边框形状
  ShapeBorder? _shape;

  //文字样式
  TextStyle? _textStyle;

  //状态图标大小
  double? _stateIconSize;
  double? _stateIconSpacing;
  Color? _stateIconColor;

  HcButtonTheme({
    Color? backgroundColor,
    Color? disabledBackgroundColor,
    Color? textColor,
    Color? disabledTextColor,
    Color? splashColor,
    Color? highlightColor,
    Color? stateIconColor,
    Size? miniSize,
    Size? maxSize,
    EdgeInsets? padding,
    BorderRadius? borderRadius,
    double? fontSize,
    double? stateIconSize,
    double? stateIconSpacing,
    FontWeight? fontWeight,
    ShapeBorder? shape,
    int? maxLines,
    BorderSide? side,
    TextStyle? textStyle,
  })  : _backgroundColor = backgroundColor,
        _disabledBackgroundColor = disabledBackgroundColor,
        _textColor = textColor,
        _disabledTextColor = disabledTextColor,
        _splashColor = splashColor,
        _highlightColor = highlightColor,
        _stateIconColor = stateIconColor,
        _miniSize = miniSize,
        _maxSize = Size(
            max(maxSize?.width ?? double.infinity, miniSize?.width ?? 0),
            max(maxSize?.height ?? double.infinity, miniSize?.height ?? 0)),
        _padding = padding,
        _borderRadius = borderRadius,
        _fontSize = fontSize,
        _stateIconSize = stateIconSize,
        _stateIconSpacing = stateIconSpacing,
        _shape = shape,
        _side = side,
        _maxLines = maxLines,
        _textStyle = textStyle,
        _fontWeight = fontWeight;

  @override
  HcButtonTheme merge(value) {
    if (value == null) return this;
    return copyWith(
      backgroundColor: value._backgroundColor,
      disabledBackgroundColor: value._disabledBackgroundColor,
      textColor: value._textColor,
      disabledTextColor: value._disabledTextColor,
      splashColor: value._splashColor,
      highlightColor: value._highlightColor,
      stateIconColor: value._stateIconColor,
      miniSize: value._miniSize,
      maxSize: Size(max(value._maxSize?.width ?? maxSize.width, miniSize.width),
          max(value._maxSize?.height ?? maxSize.height, miniSize.height)),
      padding: value._padding,
      borderRadius: value._borderRadius,
      fontSize: value._fontSize,
      stateIconSize: value._stateIconSize,
      stateIconSpacing: value._stateIconSpacing,
      fontWeight: value._fontWeight,
      shape: value._shape ??
          RoundedRectangleBorder(
              borderRadius: value.borderRadius, side: value.side),
      maxLines: value._maxLines,
      side: value._side,
      textStyle: textStyle.merge(
          TextStyle(fontSize: value._fontSize, fontWeight: value._fontWeight)
              .merge(value._textStyle)),
    );
  }

  HcButtonTheme copyWith({
    Color? backgroundColor,
    Color? disabledBackgroundColor,
    Color? textColor,
    Color? disabledTextColor,
    Color? splashColor,
    Color? highlightColor,
    Color? stateIconColor,
    Size? miniSize,
    Size? maxSize,
    EdgeInsets? padding,
    BorderRadius? borderRadius,
    double? fontSize,
    double? stateIconSize,
    double? stateIconSpacing,
    FontWeight? fontWeight,
    ShapeBorder? shape,
    BorderSide? side,
    int? maxLines,
    TextStyle? textStyle,
  }) {
    return HcButtonTheme(
      backgroundColor: backgroundColor ?? _backgroundColor,
      disabledBackgroundColor:
          disabledBackgroundColor ?? _disabledBackgroundColor,
      textColor: textColor ?? _textColor,
      disabledTextColor: disabledTextColor ?? _disabledTextColor,
      splashColor: splashColor ?? _splashColor,
      highlightColor: highlightColor ?? _highlightColor,
      stateIconColor: stateIconColor ?? _stateIconColor,
      maxSize: maxSize ?? _maxSize,
      miniSize: miniSize ?? _miniSize,
      padding: padding ?? _padding,
      borderRadius: borderRadius ?? _borderRadius,
      fontSize: fontSize ?? _fontSize,
      stateIconSize: stateIconSize ?? _stateIconSize,
      stateIconSpacing: stateIconSpacing ?? _stateIconSpacing,
      fontWeight: fontWeight ?? _fontWeight,
      shape: shape ?? _shape,
      side: side ?? _side,
      maxLines: maxLines ?? _maxLines,
      textStyle: textStyle ?? _textStyle,
    );
  }

  @override
  void initTheme({HcCommonTheme? tempTheme}) {
    super.initTheme(tempTheme: tempTheme);
    HcButtonTheme buttonTheme = HcCustomerConfig().allTheme.buttonTheme;
    _backgroundColor ??= commonTheme.defaultColor;
    _disabledBackgroundColor ??=
        backgroundColor.withOpacity(commonTheme.disabledOpacity);
    _textColor ??= HcColorUtil.needLightColor(backgroundColor)
        ? commonTheme.textInverseColor
        : commonTheme.textBaseColor;
    _disabledTextColor ??=
        textColor.withOpacity(commonTheme.disabledTextOpacity);
    _splashColor ??= buttonTheme.splashColor;
    _highlightColor ??= buttonTheme.highlightColor;
    _stateIconColor ??= commonTheme.textDisableColor;
    _miniSize ??= buttonTheme.miniSize;
    _maxSize ??= buttonTheme.maxSize;
    _padding ??= EdgeInsets.symmetric(horizontal: commonTheme.hSpacingPage);
    _borderRadius ??= BorderRadius.circular(commonTheme.radiusBase);
    _fontSize ??= commonTheme.fontSizeBase;
    _stateIconSize ??= commonTheme.iconSizeXxs;
    _fontWeight ??= buttonTheme.fontWeight;
    _side ??= BorderSide(
        color: commonTheme.borderColor, width: 0.0, style: BorderStyle.none);
    _shape ??= RoundedRectangleBorder(side: side, borderRadius: borderRadius);
    _maxLines ??= buttonTheme.maxLines;
    _textStyle ??= buttonTheme.textStyle.merge(TextStyle(
            fontWeight: buttonTheme.fontWeight, fontSize: buttonTheme.fontSize)
        .merge(_textStyle));
    _stateIconSpacing ??= commonTheme.hSpacingXxs;
  }

  Color get backgroundColor =>
      _backgroundColor ??
      HcDefaultThemeConfig.defaultButtonTheme.backgroundColor;

  Color get disabledBackgroundColor =>
      _disabledBackgroundColor ??
      HcDefaultThemeConfig.defaultButtonTheme.disabledBackgroundColor;

  Color get textColor =>
      _textColor ?? HcDefaultThemeConfig.defaultButtonTheme.textColor;

  Color get disabledTextColor =>
      _disabledTextColor ??
      HcDefaultThemeConfig.defaultButtonTheme.disabledTextColor;

  Size get miniSize =>
      _miniSize ?? HcDefaultThemeConfig.defaultButtonTheme.miniSize;

  Size get maxSize =>
      _maxSize ?? HcDefaultThemeConfig.defaultButtonTheme.maxSize;

  EdgeInsets get padding =>
      _padding ?? HcDefaultThemeConfig.defaultButtonTheme.padding;

  BorderRadius get borderRadius =>
      _borderRadius ?? HcDefaultThemeConfig.defaultButtonTheme.borderRadius;

  FontWeight get fontWeight =>
      _fontWeight ?? HcDefaultThemeConfig.defaultButtonTheme.fontWeight;

  double get fontSize =>
      _fontSize ?? HcDefaultThemeConfig.defaultButtonTheme.fontSize;

  double get stateIconSize =>
      _stateIconSize ?? HcDefaultThemeConfig.defaultButtonTheme.stateIconSize;

  double get stateIconSpacing =>
      _stateIconSpacing ??
      HcDefaultThemeConfig.defaultButtonTheme.stateIconSpacing;

  Color get splashColor =>
      _splashColor ?? HcDefaultThemeConfig.defaultButtonTheme.splashColor;

  Color get highlightColor =>
      _highlightColor ?? HcDefaultThemeConfig.defaultButtonTheme.highlightColor;

  Color get stateIconColor =>
      _stateIconColor ?? HcDefaultThemeConfig.defaultButtonTheme.stateIconColor;

  ShapeBorder get shape =>
      _shape ?? HcDefaultThemeConfig.defaultButtonTheme.shape;

  BorderSide get side => _side ?? HcDefaultThemeConfig.defaultButtonTheme.side;

  int get maxLines =>
      _maxLines ?? HcDefaultThemeConfig.defaultButtonTheme.maxLines;

  TextStyle get textStyle =>
      _textStyle ?? HcDefaultThemeConfig.defaultButtonTheme.textStyle;
}
